package cn.sciento.farm.devicemanager.api.jsonrpc.impl;

import cn.sciento.farm.devicemanager.api.jsonrpc.AreaJsonApi;
import cn.sciento.farm.devicemanager.common.constant.ResultConstant;
import cn.sciento.farm.devicemanager.common.util.ReturnUtil;
import cn.sciento.farm.devicemanager.dao.model.ResponseMessage;
import cn.sciento.farm.devicemanager.service.*;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


@Transactional(
        propagation = Propagation.REQUIRED, //根据需要修改
        timeout = -1,    //时间大于零时，超过时间未完成则事务回滚
        isolation = Isolation.DEFAULT,//隔离级别
        readOnly = false,        //是否只读 true or false
        rollbackFor = Exception.class,    //或者它异常类
        noRollbackFor = OutOfMemoryError.class    //或者它异常类
)
@Service
@AutoJsonRpcServiceImpl
public class AreaJsonApiImpl implements AreaJsonApi {

    @Autowired
    DeviceService deviceService;

    @Autowired
    CameraService cameraService;

    @Autowired
    InverterService inverterService;

    @Autowired
    WaterFertilizerService waterFertilizerService;

    @Autowired
    FertilizerChannelService fertilizerChannelService;

    @Autowired
    ScreenService screenService;


    @Override
    public ResponseMessage deleteArea(Integer areaId) {
        // 删除设备
        deviceService.deleteByArea(areaId, null);
        // 删除摄像头
        cameraService.deleteByArea(areaId, null);
        // 删除水肥机
        waterFertilizerService.deleteByAreaId(areaId);
        // 删除变频器
        inverterService.deleteByAreaId(areaId);
        // 删除屏幕
        screenService.deleteByAreaId(areaId);

        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS);
    }

    @Override
    public ResponseMessage deleteLand(Integer landId) {
        // 删除设备
        deviceService.deleteByArea(null, landId);
        // 删除摄像头
        cameraService.deleteByArea(null, landId);

        return ReturnUtil.getReturnMap(ResultConstant.SUCCESS);
    }

    @Override
    public ResponseMessage judgeDeleteArea() {
        return null;
    }

    @Override
    public ResponseMessage judgeDeleteLand() {
        return null;
    }
}
